package org.jsola.hr.web;

import java.util.List;

import javax.validation.Valid;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.AttendGoOutRecordAddDTO;
import org.jsola.hr.dto.AttendGoOutRecordUpdateDTO;
import org.jsola.hr.query.AttendGoOutRecordQuery;
import org.jsola.hr.service.IAttendGoOutRecordService;
import org.jsola.hr.vo.AttendGoOutRecordListVO;
import org.jsola.hr.vo.AttendGoOutRecordVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author zhr
 */
@RestController("hrAttendGoOutRecordController")
@RequestMapping(HrConstants.API_PATH + "/v1/attendGoOutRecords")
public class AttendGoOutRecordController implements IAttendGoOutRecordControllerAPI {

    @Autowired
    private IAttendGoOutRecordService attendGoOutRecordService;

    @Override
    @PostMapping
    public Result<AttendGoOutRecordVO> save(@Valid @RequestBody AttendGoOutRecordAddDTO attendGoOutRecordAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增外出打卡记录权限校验

        return Result.success(
                attendGoOutRecordService.save(attendGoOutRecordAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody AttendGoOutRecordUpdateDTO attendGoOutRecordUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改外出打卡记录权限校验

        int count = attendGoOutRecordService.update(attendGoOutRecordUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }



    @Override
    @DeleteMapping(value = "/{attendGoOutRecordId}")
    public Result<Integer> delete(@PathVariable String attendGoOutRecordId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除外出打卡记录权限校验

        int count = attendGoOutRecordService.deleteByIds(tokenUser, attendGoOutRecordId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] attendGoOutRecordIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除外出打卡记录权限校验

        int count = attendGoOutRecordService.deleteByIds(tokenUser, attendGoOutRecordIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{attendGoOutRecordId}")
    public Result<AttendGoOutRecordVO> findById(@PathVariable String attendGoOutRecordId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看外出打卡记录权限校验

        return Result.success(
            attendGoOutRecordService.selectById(attendGoOutRecordId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<AttendGoOutRecordListVO>> listAll(@Valid AttendGoOutRecordQuery attendGoOutRecordQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看外出打卡记录权限校验

        return Result.success(
            attendGoOutRecordService.select(attendGoOutRecordQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<AttendGoOutRecordListVO>> page(@Valid AttendGoOutRecordQuery attendGoOutRecordQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            attendGoOutRecordService.selectPage(attendGoOutRecordQuery, tokenUser.getSiteId())
        );
    }


}

